/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2012-10-24         luwei
 */

package com.coocaa.x.app.hall.main.ui.category;

import java.util.ArrayList;
import java.util.List;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.NinePatch;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.coocaa.x.app.hall.R;
import com.coocaa.x.app.hall.main.ui.category.CategoryName.Category;

import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.uipackage.SkinManager;

public class CategoryCursor extends View {

    private NinePatch mFocusCursor = null;
    private Context mContext = null;
    private List<Cursor> mCursors = null;
    private int mCurrentPos = 0;
    private int mTempX = 0;
    private int mTempY = CoocaaApplication.Div(20);
    private int mDefaultStep = CoocaaApplication.Div(60);
    private int mStep = mDefaultStep;
    private Bitmap mFocusBitmap = null;
    private Bitmap mSelectBitmap = null;
    private CategoryName mSkyCategoryName;


    public CategoryCursor(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
        // TODO Auto-generated constructor stub
    }

    public CategoryCursor(Context context) {
        super(context);
        this.mContext = context;
        setWillNotDraw(false);
        init();

    }

    public void setmSkyCategoryName(CategoryName mSkyCategoryName) {
        this.mSkyCategoryName = mSkyCategoryName;
    }

    private void init() {
        invalidate();
        mCursors = new ArrayList<Cursor>();
        mFocusBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tag_focus);

        mFocusCursor = new NinePatch(mFocusBitmap, mFocusBitmap.getNinePatchChunk(), null);
//		mSelectBitmap =  SkinManager.getInstance().getBitmap(mSelectBitmap, R.drawable.dot);
//		if (mSelectBitmap == null)
//        {
//		    mSelectBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dot);
//        }
    }

    public void addData(CategoryName.Category data) {
        Cursor cursor = creatCursor(data, mCursors.size());
        mCursors.add(cursor);
        invalidate();
    }

    private Cursor creatCursor(Category data, int index) {
        Cursor cursor = new Cursor();
        if (index == 0) {
            if (mCursors.size() > 0) {
                cursor.mDesX = mCursors.get(0).mDesX;
            } else
                cursor.mDesX = 0;//CoocaaApplication.Div(-40);
        } else {
            cursor.mDesX = mCursors.get(index - 1).mDesX
                    + CoocaaApplication.Div(90) + mCursors.get(index - 1).mDataWidth;//data.mWidth;
        }
        cursor.mDataWidth = data.mWidth;
        cursor.mLength = CoocaaApplication.Div(160) + data.mWidth;
        LogUtils.d("cursor width is " + cursor.mLength + " and cursor x is " + cursor.mDesX);
        cursor.mDesY = 0;

        return cursor;
    }

    private void refreshCursor(int index, int addOrRemove) {
        int i = 0;
        if (addOrRemove == 0) {
            for (i = index + 1; i < mCursors.size(); i++) {

                mCursors.get(i).mDesX = mCursors.get(i - 1).mDesX
                        + CoocaaApplication.Div(90) + mCursors.get(i - 1).mDataWidth;
            }
        } else {
            for (i = index; i < mCursors.size(); i++) {
                if (i == 0) {
                    mCursors.get(i).mDesX = 0;
                } else {
                    mCursors.get(i).mDesX = mCursors.get(i - 1).mDesX
                            + CoocaaApplication.Div(90) + mCursors.get(i - 1).mDataWidth;
                }

            }
        }

    }

    public void addData(Category data, int index) {
        if (mCursors.size() <= index) {
            addData(data);
            invalidate();
            return;
        }

        Cursor cursor = creatCursor(data, index);
        mCursors.add(index, cursor);
        refreshCursor(index, 0);

        if (mCurrentPos >= index) {
            mCurrentPos++;
            if (mCursors.get(index).mLength - CoocaaApplication.Div(30)
                    + mCursors.get(mCurrentPos).mDesX
                    + mCursors.get(mCurrentPos).mLength > 1400) {
                moveX(mCursors.get(index).mLength - CoocaaApplication.Div(30));
            }
        }
        invalidate();
    }

    public void removeData(int index) {
        if (mCursors.size() > index) {

            if (mCurrentPos > index && mCurrentPos > 0
                    || (mCurrentPos == mCursors.size() - 1)) {
                mCurrentPos--;
                if (mCurrentPos < 0)
                    mCurrentPos = 0;
            }
            mCursors.remove(index);
            refreshCursor(index, 1);
            invalidate();
        }
    }

    public void setData(List<Category> categories) {

        int i = mCursors.size();
        for (Category category : categories) {
            Cursor cursor = creatCursor(category, i);
            i++;
            mCursors.add(cursor);
        }
    }

    public void onDestory() {
        if (mFocusBitmap != null && !mFocusBitmap.isRecycled()) {
            mFocusBitmap.recycle();
            mFocusBitmap = null;
        }
        if (mSelectBitmap != null && mSelectBitmap.isRecycled()) {
            mSelectBitmap.recycle();
            mSelectBitmap = null;
        }

        if (mCursors != null) {
            mCursors.clear();
            mCursors = null;
        }
        mFocusCursor = null;
    }

    private boolean mIsFirst = true;

    public void setFocusPos(int pos) {
        LogUtils.d("the currentpos is " + mCurrentPos);
        LogUtils.d("the pos is " + pos);

        if (mCursors.size() == 0 || mCursors.size() <= pos) {
            return;
        }

        if (mIsFirst) {
            mStep = 200;
            mIsFirst = false;
        } else {
            if (mCurrentPos == pos) {
                mStep = mDefaultStep;
            } else {
                mStep = mDefaultStep * Math.abs(mCurrentPos - pos);
            }
        }

        this.mCurrentPos = pos;
        if (mCursors.get(mCurrentPos).mDesX < 0) {
            moveX(mCursors.get(mCurrentPos).mDesX);
        }

        if (mCursors != null && mCursors.size() > pos)
            mCursors.get(pos).beginMove();
    }

    public void setFocusPos(int pos, boolean hasAnim) {
        if (mCursors.size() == 0 || mCursors.size() <= pos) {
            return;
        }
        if (hasAnim)
            setFocusPos(pos);
        else {
            LogUtils.d("the currentpos is " + mCurrentPos);
            LogUtils.d("the pos is " + pos);
            mTempX = mCursors.get(pos).mDesX;
            this.mCurrentPos = pos;
            invalidate();
        }
    }

    public boolean setNextLeftPos() {
        if (mCurrentPos > 0) {
            mCurrentPos--;
            if (mCursors.get(mCurrentPos).mDesX < 0) {

                if (mCurrentPos == 0) {
                    moveX(mCursors.get(0).mDesX);
                } else
                    moveX(CoocaaApplication.Div(30) - mCursors.get(mCurrentPos).mLength);
            }
            mStep = mDefaultStep;
            mCursors.get(mCurrentPos).beginMove();
            return true;
        }
        return false;
    }

    public int getFocusPos() {
        return mCurrentPos;
    }

    private void moveX(int x) {
        moveX += x;
        for (Cursor cursor : mCursors) {
            cursor.mDesX -= x;
        }
        if (mCurrentPos == 0) {
            moveX = 0;
        }

        if (mSkyCategoryName != null) {
            mSkyCategoryName.setMoveX(moveX, mCurrentPos);
        }
    }

    public int getMoveX() {
        return moveX;
    }

    int moveX = 0;

    public boolean setNextRightPos() {
        if (mCurrentPos < mCursors.size() - 1) {
            mCurrentPos++;
            if (mCursors.get(mCurrentPos).mDesX + mCursors.get(mCurrentPos).mLength > 1400) {
                moveX(mCursors.get(mCurrentPos).mLength - CoocaaApplication.Div(30));
            }
            mStep = mDefaultStep;
            mCursors.get(mCurrentPos).beginMove();
            return true;
        }
        return false;
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mCursors.size() > 0) {
            Rect rect = new Rect(mTempX, mTempY, mTempX + mCursors.get(mCurrentPos).mLength, mTempY + CoocaaApplication.Div(185));
            if (isSelected()) {
//	            mSelectCursor.draw(canvas, rect);
//				canvas.drawBitmap(mSelectBitmap, mTempX + mCursors.get(mCurrentPos).mLength/2 - mTempY+CoocaaApplication.Div(10),
//						mTempY+CoocaaApplication.Div(125), null);
            } else {
                mFocusCursor.draw(canvas, rect);
            }
            if (mCursors != null) {
                mCursors.get(mCurrentPos).beginMove();
            }
        }
    }

    public class Cursor {
        public int mDesX;
        public int mDesY;
        public int mLength;
        public int mDataWidth;

        public void beginMove() {
            if (mDesX != mTempX) {
                if (mDesX - mTempX > mStep) {
                    mTempX += mStep;

                } else if (mDesX - mTempX < -mStep) {
                    mTempX -= mStep;
                } else {
                    mTempX = mDesX;

                }
                invalidate();
            }
        }
    }
}
